package com.vend.common.base
import android.content.Context
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.gyf.immersionbar.ImmersionBar
import org.greenrobot.eventbus.EventBus

/**
 * Created by glendal on 2022/01/13.
 * 创建一个基础的Activity
 */
abstract class BaseActivity<T> : AppCompatActivity() {
    lateinit var mSelf : Context
    var mPresenter : T ?= null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(getContentLayoutId())
        this.mSelf = this
        initView(savedInstanceState)
        initImmersionBar()
        checkUseEventBus()
        Log.e("tag","执行处理")
    }

    override fun onDestroy() {
        super.onDestroy()
        checkDestroyEventBus()
    }

    /**
     * 通过这个来获取布局
     */
    abstract fun getContentLayoutId():Int

    /**
     * 检测EventBus注册
     */
    private fun checkUseEventBus(){
        if (useEventBus()){
            EventBus.getDefault().register(this)
        }
    }


    /**
     * 反注册EventBus
     */
    private fun checkDestroyEventBus(){
        if (useEventBus()){
            EventBus.getDefault().unregister(this)
        }
    }


    /**
     * 初始化沉浸式
     * Init immersion bar.
     */
    open protected fun initImmersionBar() {
        //设置共同沉浸式样式
        ImmersionBar.with(this).fitsSystemWindows(true).
        statusBarColor(android.R.color.background_dark).
        navigationBarColor(android.R.color.background_dark)
            .keyboardEnable(true).init()
    }
    abstract fun initView(savedInstanceState: Bundle?)

    protected fun useEventBus(): Boolean{
        return false
    }
}